daily assignment 21

library(dataRetrieval)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(lubridate)

Attaching package: 'lubridate'
The following objects are masked from 'package:base':

    date, intersect, setdiff, union
library(tsibble)
Registered S3 method overwritten by 'tsibble':
  method               from 
  as_tibble.grouped_df dplyr

Attaching package: 'tsibble'
The following object is masked from 'package:lubridate':

    interval
The following objects are masked from 'package:base':

    intersect, setdiff, union
# Example: Cache la Poudre River at Mouth (USGS site 06752260)
poudre_flow <- readNWISdv(siteNumber = "06752260",    # Download data from USGS for site 06752260
                          parameterCd = "00060",      # Parameter code 00060 = discharge in cfs)
                          startDate = "2013-01-01",   # Set the start date
                          endDate = "2023-12-31") |>  # Set the end date
  renameNWISColumns() |>                              # Rename columns to standard names (e.g., "Flow", "Date")
  mutate(Date = yearmonth(Date)) |>                   # Convert daily Date values into a year-month format (e.g., "2023 Jan")
  group_by(Date) |>                                   # Group the data by the new monthly Date
  summarise(Flow = mean(Flow))                       # Calculate the average daily flow for each month
GET:https://waterservices.usgs.gov/nwis/dv/?site=06752260&format=waterml%2C1.1&ParameterCd=00060&StatCd=00003&startDT=2013-01-01&endDT=2023-12-31
poudre_ts <- poudre_flow %>%
  as_tsibble(index = Date)
library(ggplot2)
library(plotly)

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
p <- ggplot(poudre_ts, aes(x = Date, y = Flow)) +
  geom_line(color = "purple") +
  labs(title = "Monthly Streamflow - Cache la Poudre River",
       x = "Date", y = "Flow (cfs)")

ggplotly(p)
library(feasts)
Loading required package: fabletools
poudre_ts %>%
  gg_subseries(Flow)

library(fable)
library(fabletools)

decomp <- poudre_ts %>%
  model(STL(Flow))

decomp %>%
  components() %>%
  autoplot()

The STL decomposition shows three main patterns in the streamflow data. The trend shows a slow increase in flow from 2013 to 2015, followed by a decline through 2020, and a slight rise again after that. The seasonal component shows a strong and consistent annual cycle, with peaks in late spring or early summer probably caused by snowmelt. Lastly, the remainder shows short-term fluctuations not explained by trend or seasonality—maybe from storms, droughts, or water management events.